package com.eduadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.eduadmin.entity.Page;
import com.eduadmin.entity.Student;
import com.eduadmin.entity.Teacher;
import com.eduadmin.listener.StudentDataUploadListener;
import com.eduadmin.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    //构造函数自动装配
    private final IStudentService studentService;

    @Autowired
    public StudentController(IStudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping(value = "addStudents")
    public String addStudents(MultipartFile studentinfoFile) throws IOException {
        EasyExcel.read(studentinfoFile.getInputStream(), Student.class, new StudentDataUploadListener(studentService)).sheet().doRead();
        return "uploaddataresult";
    }
    //删除学生
    @RequestMapping(value = "deleteStudent")
    @ResponseBody
    public Map<String, String> deleteStudent(@RequestParam(value = "ids[]") String[] studentIdCard) {
        //将身份证号拼接为 ，分割的字符串 方便数据库删除操作
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<studentIdCard.length;i++){
            sb.append("'"+studentIdCard[i]+"'");
            if(i<studentIdCard.length-1){
                sb.append(",");
            }
        }
        String studentIdCardString = sb.toString();
        Map<String, String> ret = new HashMap<>();
        int n = studentService.deleteStudent(studentIdCardString);
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除成功！");
            return ret;
        }
        ret.put("type", "error");
        ret.put("msg", "删除失败！");
        return ret;
    }

    //新增学生 hgm
    @RequestMapping(value = "newAddStudent")
    @ResponseBody
    public Map<String, String> newAddStudent(Student student) {
        Map<String, String> ret = new HashMap<>();
        //拿到身份证号并查询是否存在数据库中
        String studentIdCard = student.getStudentIdCard();
        Student st = studentService.getStudentByIdcard(studentIdCard);
        if (st != null) {
            //该学生已经在数据库中
            ret.put("type", "error");
            ret.put("msg", "增加学生失败，该身份证号已经在系统中！");
            return ret;
        }
        int n = studentService.newAddStudent(student);
        if (1 != n) {
            //新增失败
            ret.put("type", "error");
            ret.put("msg", "增加失败！");
        } else {
            //新增成功
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        }
        return ret;
    }

    //编辑学生 hgm
    @RequestMapping(value = "editStudent")
    @ResponseBody
    public Map<String, String> editStudent(Student student) {
        Map<String, String> ret = new HashMap<>();
        int n = studentService.editStudent(student);
        if (n == 1) {
            //实际上执行的插入操作 身份证号是新的
            ret.put("type", "success");
            ret.put("msg", "增加教师成功！");
            return ret;
        }
        if (n == 2) {
            //更新成功
            ret.put("type", "success");
            ret.put("msg", "修改信息成功！");
            return ret;
        }
        ret.put("type", "error");
        ret.put("msg", "修改信息失败！");
        return ret;
    }


    //hgm 获取所有学生信息
    @RequestMapping(value = "getAllStudents")
    @ResponseBody
    public Map<String, Object> getAllStudents(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            @RequestParam(value = "studentName", required = false, defaultValue = "") String studentName   //页面搜索框参数
    ) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());
        paraMap.put("studentName", studentName);

        List<Student> studentList = studentService.getAllStudents(paraMap);
        Long total = studentService.getAllTotal(paraMap);
        reMap.put("rows", studentList);
        reMap.put("total", total);

        return reMap;
    }
}
